package com.dmx.cep.action.contentManage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;

import org.apache.commons.io.IOUtils;

import com.dmx.cep.entity.VodContentInfo;
import com.dmx.cep.entity.VodContentStatus;
import com.dmx.cep.service.ContentService;
import com.dmx.cep.service.ServiceMonitorService;
import com.dmx.cep.util.CaptionTextParse;
import com.dmx.cep.util.EncodingDetecter;
import com.dmx.cep.util.TextFilter;
import com.dmx.fw.web.struts2.action.CommonQueryAction;
/**
 * 
 * @author magicyang
 *	元数据字幕导入查询Action
 */
public class ContentCaptionAction extends CommonQueryAction{
	
	private String q_title;
	
	private String q_actor;
	
	private String q_director;
	
	private Integer q_status;
	
	private Date q_releaseDate;
	
	private String q_contID;
	
	private Integer q_captionSts;
	
	private Integer q_importSts;
	
	private String contentId;
	
	private ContentService contentService;
	
	private ServiceMonitorService serviceMonitorService;
	
	private VodContentInfo content;
	
	private VodContentStatus vodStatus;
	
	private File captionFile;
	
	private String result;
	
	private String fileDir;
	
	private String language;
	
	
	public String execute() throws Exception{
		language=super.getLocale().getLanguage();
        if("captionImportInput".equals(this.act)){
        	return captionImportInput();
        }
        
        if("monitorCaptionImport".equals(this.act)){
        	return monitorCaptionImport();
        }
        if("monitorCaptionInput".equals(this.act)){
        	return monitorCaptionInput();
        }
        
		if("testFileExists".equals(this.act)){
			return testFileExists();
		}	
        
        if("doCaptionImport".equals(this.act)){
        	return doCaptionImport();
        }
        
        if("captionConfirm".equals(this.act)){
        	return captionConfirm();
        }

        if("captionView".equals(this.act)){
        	return captionView();
        }
        
		if(q_importSts==null){
			this.busiId="searchContentCaption";
		}else if(q_importSts==0){
			this.busiId="searchContentCaption2";
		}else if(q_importSts==1){
			this.busiId="searchContentCaption3";
		}else if(q_importSts==2){
			this.busiId="searchContentCaption4";
		}
		
		if(this.sortField==null||"".equals(this.sortField.trim())){
			this.sortField="CREATION_DATE";
		}
		return super.execute();
	}
	
	
	public String captionImportInput() throws Exception{
		
		content=(VodContentInfo)contentService.getObject(VodContentInfo.class, contentId);
		
		return "captionImport";
	}
	
	public String monitorCaptionInput() throws Exception{
		
		content=(VodContentInfo)contentService.getObject(VodContentInfo.class, contentId);
		
		return "monitorCaptionInput";
	}
	
	public String testFileExists() throws Exception{
		log.info("file dir : "+fileDir);
		
		try {
			File f = new File(fileDir);

			if (f.exists()) {
				result="{msg:'true'}";
			}else{
				result="{msg:'false'}";
			}
		} catch (Exception e) {
			log.error(e);
			result="{msg:'false'}";
		}
		
		log.info("result : "+result);
		
		this.setAjaxResult(result);
		
		return null;
	}
	
	public String doCaptionImport() throws Exception{
		
		try{
			CaptionTextParse textParse=null;
			
			EncodingDetecter ed=new EncodingDetecter();
			String encodeType=ed.preParse(captionFile.getPath(), null);
			log.info("The import file encoding: "+encodeType);
			if(encodeType!=null){
				if (encodeType.equals("UTF-8")|| encodeType.equals("ASCII")) {
					textParse=new CaptionTextParse(new FileInputStream(captionFile),"UTF-8");
				}else if ("windows-1252".equals(encodeType)) {
					textParse=new CaptionTextParse(new FileInputStream(captionFile),"Unicode");
				}else{
					textParse=new CaptionTextParse(new FileInputStream(captionFile),encodeType);
				}
			}
			
			textParse.parse();
			
			if(textParse.isErrorFlag()){
				
				if(textParse.getErrorLine()>0){
					this.setErrorInfo(this.getText(textParse.getErrorCode())+this.getText("error.line")+textParse.getErrorLine());
				}else{
					this.setErrorInfo(this.getText(textParse.getErrorCode()));
				}
				return captionImportInput();
			}
			
			String captionData=IOUtils.toString(new FileInputStream(captionFile),encodeType);
			
			/**
			 * 过滤掉非法字符
			 */
			captionData=TextFilter.filter(captionData);
			
			
			VodContentStatus contentStatus=(VodContentStatus)contentService.getObject(VodContentStatus.class, contentId);
			contentStatus.setCaptionData(captionData);
			contentService.saveObject(contentStatus);
			
			this.setSuccessinfo(this.getText("caption.import.success"));
			return "list";
		}catch(Exception e){
			log.error(e);
			e.printStackTrace();
			this.setErrorInfo(this.getText("caption.import.failed"));
			return captionImportInput();
		}
		
	}
	
	
	public String monitorCaptionImport() throws Exception{
		try{
			CaptionTextParse textParse=null;
			
			EncodingDetecter ed=new EncodingDetecter();
			String encodeType=ed.preParse(captionFile.getPath(), null);
			log.info("The import file encoding: "+encodeType);
			if(encodeType!=null){
				if (encodeType.equals("UTF-8")|| encodeType.equals("ASCII")) {
					textParse=new CaptionTextParse(new FileInputStream(captionFile),"UTF-8");
				}else if ("windows-1252".equals(encodeType)) {
					textParse=new CaptionTextParse(new FileInputStream(captionFile),"Unicode");
				}else{
					textParse=new CaptionTextParse(new FileInputStream(captionFile),encodeType);
				}
			}
			
			textParse.parse();
			
				
			if(textParse.isErrorFlag()){
				
				if(textParse.getErrorLine()>0){
					this.setErrorInfo(this.getText(textParse.getErrorCode())+this.getText("error.line")+textParse.getErrorLine());
				}else{
					this.setErrorInfo(this.getText(textParse.getErrorCode()));
				}
				result = "captionfailed";
				return monitorCaptionInput();
			}
				
			
			String captionData=IOUtils.toString(new FileInputStream(captionFile),encodeType);
			
			/**
			 * 过滤掉非法字符
			 */
			captionData=TextFilter.filter(captionData);
			
			VodContentStatus contentStatus=(VodContentStatus)contentService.getObject(VodContentStatus.class, contentId);
			contentStatus.setCaptionData(captionData);
			contentService.saveObject(contentStatus);
			serviceMonitorService.reloadVodCaptionInfoStatus(contentId);
			result = "captionsuccess";
			this.setSuccessinfo(this.getText("caption.import.success"));
			return monitorCaptionInput();
		}catch(Exception e){
			log.error(e);
			e.printStackTrace();
			this.setErrorInfo(this.getText("caption.import.failed"));
			return "monitorCaptionInput";
		}
	}
	
	public String captionConfirm() throws Exception{
		
		try{
			VodContentStatus contentStatus=(VodContentStatus)contentService.getObject(VodContentStatus.class, contentId);
			contentStatus.setCaptionStatus(4);
			contentService.saveObject(contentStatus);
			
			this.setSuccessinfo(this.getText("caption.confirm.success"));
			return "list";
		}catch(Exception e){
			this.setErrorInfo(this.getText("caption.confirm.failed"));
			
			return captionImportInput();
		}
		
	}	
	
	public String captionView() throws Exception{
		
		try{
			vodStatus=(VodContentStatus)contentService.getObject(VodContentStatus.class, contentId);
			
			return "captionView";
		}catch(Exception e){
			log.error(e);
		}
		return null;
	}	
	
	
	private Reader convertFile(String encodeType, File file)
			throws FileNotFoundException, IOException {

		if ("windows-1252".equals(encodeType)) {
			encodeType = "Unicode";
		}
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis, encodeType);

		Reader in = new BufferedReader(isr);
		return in;
	}

	public String getQ_title() {
		return q_title;
	}

	public void setQ_title(String q_title) {
		this.q_title = q_title;
	}

	public String getQ_actor() {
		return q_actor;
	}

	public void setQ_actor(String q_actor) {
		this.q_actor = q_actor;
	}

	public String getQ_director() {
		return q_director;
	}

	public void setQ_director(String q_director) {
		this.q_director = q_director;
	}

	public Integer getQ_status() {
		return q_status;
	}

	public void setQ_status(Integer q_status) {
		this.q_status = q_status;
	}

	public Date getQ_releaseDate() {
		return q_releaseDate;
	}

	public void setQ_releaseDate(Date date) {
		q_releaseDate = date;
	}

	public String getQ_contID() {
		return q_contID;
	}

	public void setQ_contID(String q_contid) {
		q_contID = q_contid;
	}

	public Integer getQ_captionSts() {
		return q_captionSts;
	}

	public void setQ_captionSts(Integer sts) {
		q_captionSts = sts;
	}


	public String getContentId() {
		return contentId;
	}


	public void setContentId(String contentId) {
		this.contentId = contentId;
	}


	public ContentService getContentService() {
		return contentService;
	}


	public void setContentService(ContentService contentService) {
		this.contentService = contentService;
	}


	public VodContentInfo getContent() {
		return content;
	}


	public void setContent(VodContentInfo content) {
		this.content = content;
	}


	public File getCaptionFile() {
		return captionFile;
	}


	public void setCaptionFile(File captionFile) {
		this.captionFile = captionFile;
	}


	public VodContentStatus getVodStatus() {
		return vodStatus;
	}


	public void setVodStatus(VodContentStatus vodStatus) {
		this.vodStatus = vodStatus;
	}


	public Integer getQ_importSts() {
		return q_importSts;
	}


	public void setQ_importSts(Integer sts) {
		q_importSts = sts;
	}


	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public String getFileDir() {
		return fileDir;
	}


	public void setFileDir(String fileDir) {
		this.fileDir = fileDir;
	}


	public void setServiceMonitorService(ServiceMonitorService serviceMonitorService) {
		this.serviceMonitorService = serviceMonitorService;
	}


	public void setLanguage(String language) {
		this.language = language;
	}


	public String getLanguage() {
		return language;
	}
}
